﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace 一键注册
{
    class Program
    {
        public delegate int DllRegisterHandler();
      
        
        static void Main(string[] args)
        {
            string msg = "";
            string dllFunction = "DllRegisterServer";
            List<string> dllPathList = new List<string>();

            string[] dllPathList1 = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory,"*.ocx", System.IO.SearchOption.TopDirectoryOnly);
            string[] dllPathList2 = System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll", System.IO.SearchOption.TopDirectoryOnly);

            dllPathList.AddRange(dllPathList1);
            dllPathList.AddRange(dllPathList2);
            if (dllPathList != null && dllPathList.Count > 0)
            {
                foreach (var dllPath in dllPathList)
                {
                    object o = DynamicLoadDll.DynamicInvoke(dllPath, dllFunction, typeof(DllRegisterHandler), out msg);
                    if (o != null && (int)o >= 0)
                    {
                        //Console.WriteLine(dllPath + "注册成功！返回值："+o);
                        Console.WriteLine(dllPath + " register success! return value:" + o);
                    }
                    else
                    {
                        //Console.WriteLine(dllPath + "注册失败"+"返回值"+o+",原因：" + msg);
                        Console.WriteLine(dllPath + " register fail! return value:" + o + ",reason:" + msg);
                    }
                }
            }
            //Console.WriteLine("全部工作完成！按任意键退出...");
            Console.WriteLine("finish! press any key to quit...");
            Console.Read();
        }
    }


    public class Test
    {
        [DllImport("COMDLG32.OCX")]
        public static extern int DllRegisterServer();

    }

}
